package com.gxa.ssm340.controller;

import com.gxa.ssm340.common.R;
import com.gxa.ssm340.entity.dto.RegistDTO;
import com.gxa.ssm340.entity.pojo.User;
import com.gxa.ssm340.exception.BizException;
import com.gxa.ssm340.service.UserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @BelongsProject: huanlianchaoshi
 * @Author: Donglu
 * @CreateTime: 2023-10-23  11:25
 */
@Api(value = "UserLoginController",tags = "用户管理接口")
@RequestMapping("/user")
@RestController
public class UserRegController {

    @Autowired
    private UserLoginService userLoginService;


    private String myCaptcha;

    @ApiOperation("验证码")
    @PostMapping("/send")
    public R sendCode(@RequestParam("phone") String phone){
        User user = userLoginService.selectUserByPhone(phone);
        if (user==null){
            String uuid = UUID.randomUUID().toString().replace("-","");
            myCaptcha = uuid.substring(0,6);
            System.out.println("验证码："+myCaptcha);
            return R.success(myCaptcha);
        }
        return R.failed();
    }

    @ApiOperation(value = "用户注册",notes = "{\n" +
            "  \"name\": \"string\",\n" +
            "  \"phone\": \"13542254568\"\n" +
            "}")
    @PostMapping("/reg")
    public R reg(@RequestBody RegistDTO registDTO) throws BizException {

        System.out.println("验证码："+myCaptcha);
        /*String s = "123456";*/
        if (registDTO.getCaptcha().equals(myCaptcha)){
            RegistDTO reg = userLoginService.reg(registDTO);
            return R.success(reg);
        }
        return R.failed();
    }
}
